package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.itheima.constant.MessageConstant;
import com.itheima.easyexcellistener.EasyExcelListener;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("/orderSetting")
@Controller
@ResponseBody
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")//将文件进行上传
    public Result upload(MultipartFile excelFile){
        boolean flag=false;
        String message=null;
        try {
            EasyExcelListener easyExcelListener = new EasyExcelListener();
            EasyExcel.read(excelFile.getInputStream(), OrderSetting.class, easyExcelListener).sheet().doRead();
            List<OrderSetting> orderSettingList = easyExcelListener.getOrderSettingList();
            flag=true;
            message= MessageConstant.IMPORT_ORDERSETTING_SUCCESS;
            //将文件中的信息存入到数据库中
            orderSettingService.upload(orderSettingList);
        } catch (IOException e) {
            message= MessageConstant.IMPORT_ORDERSETTING_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/downloadFile")
    public Result downloadFile(){
        boolean flag=false;
        String message=null;
        try {
            //去数据库中读取数据进行封装
            List<OrderSetting>orderSettingList=orderSettingService.findAllOrderSetting();
            EasyExcel.write("F:\\itheima\\itheimasucheng\\itemOne\\health_backend\\src\\main\\webapp\\template\\ordersetting_File.xlsx", OrderSetting.class).sheet("模板").doWrite(orderSettingList);
            flag=true;
            message=MessageConstant.DOWNLOAD_ORDERSETTING_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.DOWNLOAD_ORDERSETTING_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message);
    }

    @RequestMapping("/findPage")
    public Result findPage(String stringDate){
        boolean flag=false;
        String message=null;
        List<Map>mapList= null;
        try {
            mapList = orderSettingService.findPage(stringDate);
            flag=true;
            message=MessageConstant.GET_ORDERSETTING_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.GET_ORDERSETTING_FAIL;
            e.printStackTrace();
        }
        return new Result(flag,message,mapList);
    }

    @RequestMapping("/setting")
    public Result Setting(@RequestBody OrderSetting orderSetting){
        boolean flag=false;
        String message=null;
        try {
            orderSettingService.setting(orderSetting);
            flag=true;
            message=MessageConstant.ORDERSETTING_SUCCESS;
        } catch (Exception e) {
            message=MessageConstant.ORDERSETTING_FAIL;
            e.printStackTrace();
        }

        return new Result(flag,message);
    }
}
